<?php
namespace Think\Template\TagLib;
use Think\Template\TagLib;
/**
 * TP标签库解析类
 */
class Tp extends TagLib {
    // 标签定义
    protected $tags   =  array(
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'list'      => array('attr'=>'name,cid,where,field,limit,order,key,id','level'=>3),
        'treelist'      => array('attr'=>'list,myid','level'=>1),
    );
        
    public function _list($tag,$content){
			$id = !empty($tag['id'])?$tag['id']:'r';  //定义数据查询的结果存放变量
			$key    = !empty($tag['key'])?$tag['key']:'i';
			$mod    = isset($tag['mod'])?$tag['mod']:'2';
            
            $module = $tag['name'];
            $field = $tag['field'] ? $tag['field'] : '*';
            $order = $tag['order'] ? $tag['order'] : 'id desc';
            $limit = $tag['limit'] ? $tag['limit'] : '10';
            $where = $tag['where'] ? $tag['where'] : ' 1 ';
            
			$cids = get_son_category($tag['cid']);
			
            if($module){
                if($cids){
                    $where .= " and cid in (".$cids.")";
                }
                $sql  = "D(\"{$module}\")->GetListByMap(\"{$where}\",\"{$field}\",\"{$limit}\",\"{$order}\")";
            }else{
                if($tag['sql']){
                    $sql = "M()->query(\"{$tag['sql']}\")";
                }else{
                    return "";
                }
            }
            
			//下面拼接输出语句
			$parsestr  = '';
			$parsestr .= '<?php  $_result='.$sql.'; if ($_result): $'.$key.'=0;';
			$parsestr .= 'foreach($_result as $key=>$'.$id.'):';
			$parsestr .= '++$'.$key.';$mod = ($'.$key.' % '.$mod.' );?>';
			$parsestr .= $content;//解析在article标签中的内容
			$parsestr .= '<?php endforeach; endif;?>';
			return  $parsestr;
    }
}